import { defineStore } from 'pinia'
// 引入数据类型
import type { loginForm, loginResData } from '@/api/user/type'
import { reqlogin } from '@/api/user/index'
import type { UserState } from './types/type'
import { SET_TOKEN, GET_TOKEN } from '../../utils/token'

let useUserStore = defineStore('User', {
  state: (): UserState => {
    return {
      token: GET_TOKEN(),
    }
  },
  actions: {
    async userLogin(userData: loginForm) {
      let result: loginResData = await reqlogin(userData)
      // console.log(result);
      if (result.code == 200) {
        this.token = result.data.token as string
        // localStorage.setItem("token",(result.data.token as string));//持久化
        SET_TOKEN(result.data.token as string)
        return 'ok'
      } else {
        console.log('error')
        return Promise.reject(new Error(result.data.message))
      }
    },
  },
  getters: {},
})
export default useUserStore
